// 请求接口
// #ifdef MP-WEIXIN
const commoneUrl = "https://appexpert.xinweint.cn/yb/";
// #endif
// #ifdef H5
const commoneUrl = "/api";
// #endif
const fileUrl = "https://appexpert.xinweint.cn/yb/"
//post请求封装
function postAction(url, data) {
	return apiAction('post', url, data)
}

function getAction(url, data) {
	return apiAction('get', url, data)
}

function apiAction(method, url, data) {
	console.log('url:' + url);
	return new Promise((resolve, reject) => {
		uni.request({
			url: commoneUrl + url,
			data: data,
			method: method,
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				token: uni.getStorageSync('token')
			},
			success: function(res) {
				if (res.data.code === 403) {
					uni.setStorageSync('token', '')
					uni.reLaunch({
						url: '/pages/login/index'
					})
				} else if (res.statusCode === 200) {
					resolve(res.data);
				} else {
					uni.hideLoading()
					uni.showToast({
						title: '系统异常',
						icon: 'error'
					})
					reject(res.data)
				}
			},
			error: function(e) {
				reject('网络出错');
			}
		})
	})
}

function uploadAction(url, data) {
	console.log(url, data);
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: commoneUrl + url, //仅为示例，非真实的接口地址
			filePath: data.filePath,
			name: data.name || 'file',
			formData: data.formData || {},
			header: {
				token: uni.getStorageSync('token')
			},
			success: (res) => {
				console.log('上传', res);
				resolve(typeof res.data === 'string' ? JSON.parse(res.data) : res.data)
			}
		});
	})
}
module.exports = {
	uploadAction,
	postAction,
	getAction,
	commoneUrl,
	fileUrl
}